package com.sky.controller.User;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")
@Slf4j
@Api(tags = "菜品管理")
public class DishController {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    DishService dishService;

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list(Long categoryId) {
        //1.查询redis缓存中是否存在菜品列表数据
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get("dish_" + categoryId);
        //2.如果存在，直接返回
        if (list != null && list.size() > 0) {
            //读取缓存数据,返回客服端
            return Result.success(list);
        }

        //3.如果不存在，查询mysqol中的菜品列表
        list = dishService.list(categoryId);


        //4.将mysql中查询到的菜品列表，同步到缓存中
        redisTemplate.opsForValue().set("dish_" + categoryId, list);

        return Result.success(list);
    }

}